package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.common.constant.AutocodeKeyC;
import com.my.zl.console.dao.autocode.ZlTableColumnMapper;
import com.my.zl.console.entity.autocode.ZlTableColumn;
import com.my.zl.console.po.autocode.tableColumn.ZlTableColumnVo;
import com.my.zl.console.dto.autocode.tableColumn.ReqTableColumnAddDTO;
import com.my.zl.console.dto.autocode.tableColumn.ReqTableColumnDelDTO;
import com.my.zl.console.dto.autocode.tableColumn.ReqTableColumnEditDTO;
import com.my.zl.console.dto.autocode.tableColumn.ReqTableColumnQueryDTO;
import com.my.zl.console.dto.autocode.tableColumn.ReqTableColumnDetailDTO;
import com.my.zl.console.dto.autocode.tableColumn.ReqTableColumnStateDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnAddDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnDelDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnEditDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnQueryDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnDetailDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnStateDTO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnVO;
import com.my.zl.console.dto.autocode.tableColumn.ResTableColumnVO2;
import com.my.zl.console.service.autocode.ITableColumnService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 数据库表属性信息
 * @author ZL
 */
@Service
public class TableColumnServiceImpl extends BaseManager implements ITableColumnService {

    @Autowired
    private ZlTableColumnMapper tableColumnMapper;

    @Override
    public ResTableColumnQueryDTO queryTableColumnList(ReqTableColumnQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResTableColumnQueryDTO resDTO = BeanUtil.copyProperties(page, ResTableColumnQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getColumnName())){
            reqInfo.setColumnName("%"+reqInfo.getColumnName()+"%");
        }
        if(StringUtils.isNotBlank(reqInfo.getColumnTitle())){
            reqInfo.setColumnTitle("%"+reqInfo.getColumnTitle()+"%");
        }
        if(StringUtils.isNotBlank(reqInfo.getColumnCode())){
            reqInfo.setColumnCode("%"+reqInfo.getColumnCode()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlTableColumnVo> list = tableColumnMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResTableColumnVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResTableColumnDetailDTO queryTableColumnDetail(ReqTableColumnDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResTableColumnDetailDTO res = new ResTableColumnDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlTableColumn view = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResTableColumnAddDTO insertTableColumnInfo(ReqDTO<ReqTableColumnAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnAddDTO reqData = reqInfo.getReqData();
        ResTableColumnAddDTO res = new ResTableColumnAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlTableColumn record = new ZlTableColumn();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setColumnId(IdUtil.simpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("columnId",record.getColumnId());
        //检查主键是否冲突
        if(tableColumnMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        tableColumnMapper.insertSelective(record);
        
        ZlTableColumn keyInfo = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTableColumnEditDTO updateTableColumnInfo(ReqDTO<ReqTableColumnEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnEditDTO reqData = reqInfo.getReqData();
        ResTableColumnEditDTO res = new ResTableColumnEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlTableColumn record = new ZlTableColumn();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        tableColumnMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("columnId",record.getColumnId());
        ZlTableColumn keyInfo = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTableColumnDelDTO deleteTableColumnInfo(ReqDTO<ReqTableColumnDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnDelDTO reqData = reqInfo.getReqData();
        ResTableColumnDelDTO res = new ResTableColumnDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        tableColumnMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResTableColumnStateDTO updateTableColumnState(ReqDTO<ReqTableColumnStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTableColumnStateDTO reqData = reqInfo.getReqData();
        ResTableColumnStateDTO res = new ResTableColumnStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlTableColumn record = new ZlTableColumn();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        tableColumnMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("columnId",record.getColumnId());
        ZlTableColumn keyInfo = tableColumnMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}